package edu.nf.hotel.admindao.impl;

import edu.nf.hotel.admin.AdminUser;
import edu.nf.hotel.admindao.AdminUserDao;
import edu.nf.hotel.user.User;
import edu.nf.hotel.util.ConnUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanHandler;
import org.nf.db.util.result.BeanListHandler;

import java.util.List;

/**
 * @Date 2023-06-16
 * @Author 陈
 */
public class AdminUserDaoImpl implements AdminUserDao {
    /**
     * 根据管理员名实现登录
     * @param useName
     * @return
     */
    @Override
    public AdminUser select(String useName) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select adm_id, adm_name, adm_passwords, adm_sex, adm_phone, adm_times from admin_user where adm_name=?" ;
        BeanHandler<AdminUser> handler =new BeanHandler<>(AdminUser.class);
        AdminUser adminUser = executor.executeQuery(sql,handler,useName);
        return adminUser;
    }

    /**
     * 根据管理员ID进行查询
     * @param aid
     * @return
     */
    @Override
    public AdminUser select(int aid) {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "select adm_id, adm_name, adm_passwords, adm_sex, adm_phone, adm_times from admin_user where adm_id=?";
        //创建BeanHandler，用于将查询的结果封装为实体对象
        BeanHandler<AdminUser> handler = new BeanHandler<>(AdminUser.class);
        AdminUser adminUser = executor.executeQuery(sql, handler, aid);
        adminUser.getAdmId();
        adminUser.getAdmUserName();
        adminUser.getAdmSex();
        adminUser.getAdmPhone();
        adminUser.getAdmTime();
        return adminUser;
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<AdminUser> selectAdmin() {
        SqlExecutor executor = new SqlExecutor(ConnUtils.getConnection());
        BeanListHandler<AdminUser> handler = new BeanListHandler<>(AdminUser.class);
        String sql = "select adm_id, adm_name, adm_passwords, adm_sex, adm_phone, adm_times from admin_user";
        List<AdminUser> list = executor.executeQuery(sql, handler);
        list.forEach(user -> System.out.println(user.getAdmUserName()));
        return list;
    }

    @Override
    public void delete(int rid) {
        SqlExecutor sqlExecutor = new SqlExecutor(ConnUtils.getConnection());
        BeanHandler<AdminUser> handler = new BeanHandler<>(AdminUser.class);
        String sql = "delete  from admin_user where adm_id=?";
        sqlExecutor.executeQuery(sql, handler);
    }

    @Override
    public void insert(AdminUser adminUser) {
        SqlExecutor sqlExecutor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "insert into admin_user(adm_name, adm_passwords, adm_sex, adm_phone) values(?,?,?,?)";
        sqlExecutor.executeUpdate(sql,adminUser.getAdmUserName(),adminUser.getAdmPassword(),adminUser.getAdmSex(),adminUser.getAdmPhone());
    }

    @Override
    public void update(AdminUser adminUser) {
        SqlExecutor sqlExecutor = new SqlExecutor(ConnUtils.getConnection());
        String sql = "update admin_user set adm_name=?,adm_passwords=?,adm_sex=?,adm_phone=? where adm_id=?";
        sqlExecutor.executeUpdate(sql,adminUser.getAdmUserName(),adminUser.getAdmPassword(),adminUser.getAdmSex(),adminUser.getAdmPhone());
    }
}
